package com.example.myapplication001;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;

    private ImageButton imageButton1;
    private ImageButton imageButton2;
    private ImageButton imageButton3;
    private ImageButton imageButton4;

    private TextView textView1;
    private TextView textView2;
    private TextView textView3;
    private TextView textView4;

    private Fragment fragment1 = new Fragment1();
    private Fragment fragment2 = new Fragment2();
    private Fragment fragment3 = new Fragment3();
    private Fragment fragment4 = new Fragment4();

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.mainlayout);

        initView();
        initEvent();
        initFragment();
        selectFragment(0);//默认首页是聊天界面
    }

    private void initFragment(){
        fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,fragment1);
        transaction.add(R.id.content,fragment2);
        transaction.add(R.id.content,fragment3);
        transaction.add(R.id.content,fragment4);
        transaction.commit();
    }

    private void initView(){
        linearLayout1 = findViewById(R.id.LinearLayout1);
        linearLayout2 = findViewById(R.id.LinearLayout2);
        linearLayout3 = findViewById(R.id.LinearLayout3);
        linearLayout4 = findViewById(R.id.LinearLayout4);

        imageButton1 = findViewById(R.id.ImageButton1);
        imageButton2 = findViewById(R.id.ImageButton2);
        imageButton3 = findViewById(R.id.ImageButton3);
        imageButton4 = findViewById(R.id.ImageButton4);

        textView1 = findViewById(R.id.TextView1);
        textView2 = findViewById(R.id.TextView2);
        textView3 = findViewById(R.id.TextView3);
        textView4 = findViewById(R.id.TextView4);
    }

    private void hideFragment(FragmentTransaction transaction){
        transaction.hide(fragment1);
        transaction.hide(fragment2);
        transaction.hide(fragment3);
        transaction.hide(fragment4);
    }

    @SuppressLint("ResourceAsColor")
    private void selectFragment(int i){//i是一个选择器，用于选择显示哪个界面
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);//首先隐藏所有页面

        switch (i){
            case 0:
                transaction.show(fragment1);
                imageButton1.setImageResource(R.drawable.picked);
                textView1.setTextColor(this.getResources().getColor(R.color.pickText));
                break;
            case 1:
                transaction.show(fragment2);
                imageButton2.setImageResource(R.drawable.picked);
                textView2.setTextColor(this.getResources().getColor(R.color.pickText));
                break;
            case 2:
                transaction.show(fragment3);
                imageButton3.setImageResource(R.drawable.picked);
                textView3.setTextColor(this.getResources().getColor(R.color.pickText));
                break;
            case 3:
                transaction.show(fragment4);
                imageButton4.setImageResource(R.drawable.picked);
                textView4.setTextColor(this.getResources().getColor(R.color.pickText));
                break;
            default:
                break;
        }
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        resetBtn();
        switch (view.getId()){
            case R.id.LinearLayout1:
                selectFragment(0);
                break;
            case R.id.LinearLayout2:
                selectFragment(1);
                break;
            case R.id.LinearLayout3:
                selectFragment(2);
                break;
            case R.id.LinearLayout4:
                selectFragment(3);
                break;
            default:
                break;
        }
    }

    @SuppressLint("ResourceAsColor")
    private void resetBtn(){
        imageButton1.setImageResource(R.drawable.normol);
        imageButton2.setImageResource(R.drawable.normol);
        imageButton3.setImageResource(R.drawable.normol);
        imageButton4.setImageResource(R.drawable.normol);

        textView1.setTextColor(this.getResources().getColor(R.color.nopickText));
        textView2.setTextColor(this.getResources().getColor(R.color.nopickText));
        textView3.setTextColor(this.getResources().getColor(R.color.nopickText));
        textView4.setTextColor(this.getResources().getColor(R.color.nopickText));
    }

    //仅仅对bottom的四个linearlayout监听
    private void initEvent(){
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);
    }
}